<!--
 * @Author: weikang
 * @Description:
 * @Date: 2024-01-03 21:50:05
 * @LastEditTime: 2024-02-25 22:19:00
 * @FilePath: \env-manager-pc\src\views\document\device\detail\model\fuwei.vue
-->
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import documentApi from '@/api/modules/document'

const { deviceId } = defineProps({
  deviceId: {
    type: String,
    default: '',
  },
})

enum CheckType {
  restart = 'restart',
  init = 'init',
}

const timeData = ref('')
const checkList = ref<CheckType[]>([])
const startLoading = ref(false)
const duiLoading = ref(false)

function doInit() {
  if (checkList.value.length === 0) {
    return ElMessage.warning('请选择初始化内容')
  }
  if (checkList.value.includes(CheckType.restart)) {
    startLoading.value = true
    documentApi.DeviceRestart({
      deviceObjID: deviceId,
    }).then(() => {
      ElMessage.success('设备重启成功')
      startLoading.value = false
    }, (err) => {
      startLoading.value = false
      console.error(err)
    })
  }
  if (checkList.value.includes(CheckType.init)) {
    startLoading.value = true
    documentApi.DeviceInit({
      deviceObjID: deviceId,
    }).then(() => {
      startLoading.value = false
      ElMessage.success('设备初始化成功')
    }, (err) => {
      startLoading.value = false
      console.error(err)
    })
  }
}

function CallTime() {
  duiLoading.value = true
  documentApi.DeviceCallDateTime({
    deviceObjID: deviceId,
  }).then((res) => {
    timeData.value = res
    duiLoading.value = false
  }, (err) => {
    duiLoading.value = false
    console.error(err)
  })
}

function SetTime() {
  if (timeData.value === '') {
    return ElMessage.warning('请选择时间')
  }
  duiLoading.value = true
  documentApi.DeviceSetDateTime({
    deviceObjID: deviceId,
    dateTime: timeData.value,
  }).then(() => {
    duiLoading.value = false
    ElMessage.success('下发成功')
  }, (err) => {
    duiLoading.value = false
    console.error(err)
  })
}
</script>

<template>
  <div>
    <el-card class="box-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>初始化</span>
          <el-button type="primary" link :loading="startLoading" @click="doInit">
            下发
          </el-button>
        </div>
      </template>
      <el-checkbox-group v-model="checkList" v-loading="startLoading">
        <el-checkbox :label="CheckType.restart">
          硬件初始化（重启）
        </el-checkbox>
        <el-checkbox :label="CheckType.init">
          参数及数据区初始化
        </el-checkbox>
      </el-checkbox-group>
    </el-card>
    <el-card class="mt-20px" shadow="never">
      <template #header>
        <div class="card-header">
          <span>对时</span>
          <div>
            <el-button type="primary" link :loading="duiLoading" @click="CallTime">
              召测
            </el-button>
            <el-button type="warning" link :loading="duiLoading" @click="SetTime">
              下发
            </el-button>
          </div>
        </div>
      </template>
      <div>
        <div v-loading="duiLoading" class="flex flex-items-center">
          <span class="w-80px">时钟：</span> <el-date-picker
            v-model="timeData"
            type="datetime"
            placeholder="请选择时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.card-header {
  --at-apply: flex flex-justify-between;
}
</style>
